$(function() {
	FastClick.attach(document.body);
});

var superDetailVue = new Vue({
	el: "body",
	data: {
		detailJson: "", //当前任务的进行到的那个流程详情（对象）
		commentList: [], //当前任务的评论
		userId: "", //微信userid
		userName: "", //微信名字
		isDUBAN: false,
		isJINGBAN: false,
		isLeader: false, //是否是馆长
		leaderList: "",
		realId: "", //  该条任务的唯一标识
		fileList: []
	},
	created: function() {
		_this = this //防止出现闭包
		var taskID = getUrlItem("TASKID");
		var wxId = getUrlItem("userid");
		_this.userName = $.cookie("BHCJ_wechat_wechatname"); //获取当前人名
		_this.userId = getUrlItem("userid"); //获取当前id       
		$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getTaskDetail?callback=?&taskcode=" + taskID, function(jsondata) {
			_this.realId = jsondata.ID
			//对当前时间数据进行格式化
			if(jsondata.CHANGES == null) {

			} else {
				jsondata.CHANGES.forEach(function(item) {
					item.CDATE = item.CDATE.replace(/T/g, " ") //解决返回时间型带T字符
					item.CDATE = item.CDATE.replace(/-/g, "/") //解决ios显示时间型数据乱码
					var time = new Date(item.CDATE)
					item.CDATE2 = time.getFullYear() + "-" + ((time.getMonth() + 1) > 10 ? (time.getMonth() + 1) : "0" + (time.getMonth() + 1)) + "-" + (time.getDate() > 10 ? time.getDate() : "0" + time.getDate())
				});
			}
			jsondata.BEGINDATE = jsondata.BEGINDATE.replace(/-/g, "/");
			if(jsondata.ENDDATE == null) {

			} else {
				jsondata.ENDDATE = jsondata.ENDDATE.replace(/-/g, "/");
			}

			//判断当前用户是否是任务的经办人或者督办人
			if(jsondata.TASKINMANID != null) {
				var taskList = jsondata.TASKINMANID.split(",")
				for(var i = 0; i < taskList.length; i++) {
					if(taskList[i] == _this.userId) {
						_this.isJINGBAN = true
					}
				}
			}

			if(jsondata.DUBANMANID != null) {
				var duBanList = jsondata.DUBANMANID.split(",")
				for(var i = 0; i < duBanList.length; i++) {
					if(duBanList[i] == _this.userId) {
						_this.isDUBAN = true
					}
				}
			}

			//进行是否点赞的判断
			if(jsondata.LIKES != null) {
				jsondata.ISLIKE = false;
				jsondata.LIKENUM = jsondata.LIKES.length;
				jsondata.LIKEMANLIST = ""
				jsondata.LIKES.forEach(function(like) {
					jsondata.LIKEMANLIST += like.LIKEMANNAME + ","
					if(like.LIKEMANID == _this.userId) {
						jsondata.ISLIKE = true;
					}
				})
				jsondata.LIKEMANLIST = jsondata.LIKEMANLIST.substring(0, jsondata.LIKEMANLIST.length - 1)
			} else {
				jsondata.ISLIKE = false;
				jsondata.LIKENUM = 0;
			}

			jsondata.WORKLIMIT = countDayNum(jsondata.BEGINDATE, jsondata.ENDDATE)
			_this.detailJson = jsondata;
			$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Attachment.asmx/getAttachments?callback=?&id=" + _this.realId, function(jsondata) {
				jsondata.forEach(function(item) {
					item.URL = "http://47.92.116.158:8099/BhGgjSite/uploadattach/" + item.FILENAME
				})
				_this.fileList = jsondata
				console.log(_this.fileList)
			});
		});

		$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getHeaderUsers?callback=?", function(jsondata) {
			jsondata.forEach(function(item) {
				_this.leaderList += item.CODE + ","
			})
			_this.leaderList = _this.leaderList.substring(0, _this.leaderList.length - 1)
			_this.leaderList = _this.leaderList.split(",")
			for(var i = 0; i < _this.leaderList.length; i++) {
				if(_this.leaderList[i] == _this.userId) {
					_this.isLeader = true
				}
			}
		});

		$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getTaskComment?callback=?&taskcode=" + taskID, function(jsondata) {
			jsondata.forEach(function(item) {
				item.isLeader = false
				for(var i = 0; i < _this.leaderList.length; i++) {
					if(_this.leaderList[i] == item.DUUSERID) {
						item.isLeader = true
					}
				}
				item.DUBANMAN = item.DUBANMAN + "：";
				item.DUBANDATE = item.DUBANDATE.replace(/T/g, " ");
			});
			_this.commentList = jsondata;
		});

		_this.$nextTick(function() {
			setTimeout(function() {
				$(".history-detail-body").removeClass("hidden")
				$(".history-detail-action").removeClass("hidden")
				$(".loader").addClass("hidden")
			}, 200)
		})

	},
	methods: {
		checkCondition: function(a, b) {
			location.href = "../supervise/superviseDetailCondition.html?TASKID=" + b + "&index=" + a
		},
		back: function() {
			window.history.go(-1); //返回上一页
		}
	}

})

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

function countDayNum(begin, end) {
	s1 = new Date(begin);
	s2 = new Date(end);
	var days = s2.getTime() - s1.getTime();
	var time = parseInt(days / (1000 * 60 * 60 * 24));
	return time
}